﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Reflection;
using DesignPatternDefinitions;


namespace dpatoolkit.Extensions
{
    class DPAExtensions
    {
        private DPAExtensions() { /* Prevent instantiation */ }

        private static IList<IDPACodeGenerator> codeGenerators;
        private static IList<IReverseEngineer> reverseEnggs;

        static DPAExtensions()
        {
            codeGenerators = new List<IDPACodeGenerator>();
            reverseEnggs = new List<IReverseEngineer>();
        }

        public static int ScanForExtensions(string dllPath)
        {
            // Load the asm.
            int pluginsFound = 0;
            Assembly dll;
            try
            {
                dll = Assembly.LoadFrom(dllPath);
            }
            catch (Exception exp)
            {
                dpatoolkit.UILayer.ErrorMessage.Show("Error while loading DLL :\n " + dllPath + "\nMessage:" + exp.Message);
                return 0;
            }

            foreach (Type type in dll.GetTypes())
            {
                System.Windows.Forms.MessageBox.Show(type.FullName);
                if (type is IDPACodeGenerator)
                {
                    /* Found a code generator plugin */
                    IDPACodeGenerator codeGen = dll.CreateInstance(type.FullName) as IDPACodeGenerator;
                }
                else if (type is IReverseEngineer)
                {
                    /* Found a reverse engineer plugin */
                    IReverseEngineer revEngg = dll.CreateInstance(type.FullName) as IReverseEngineer;
                }
            }

            return pluginsFound;
        }

        public static IList<IDPACodeGenerator> CodeGenerators
        {
            get
            {
                return codeGenerators;
            }
        }

        public static IList<IReverseEngineer> ReverseEngineers
        {
            get
            {
                return reverseEnggs;
            }
        }

        /*
        public static IList<IDiagramTheme> CodeGenerators
        {
            get
            {
            }
        }
         * */
    }
}
